package com.fz.controller;

import com.fz.com.fz.service.BookServiceImpl;
import com.fz.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by David on 2017-06-30.
 */
@Controller
public class IndexController {
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest req){
        BookServiceImpl bookdao = new BookServiceImpl();
        if(req.getParameter("id")!=null){
            int id = Integer.parseInt(req.getParameter("id"));
            bookdao.deleteById(id);
        }
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("books",bookdao.queryAll());
        return mv;
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    public IndexController() {
    }

    @RequestMapping(value="/save",method = RequestMethod.POST)
    public String save(Book book){
        BookServiceImpl bookdao = new BookServiceImpl();
        bookdao.add(book);
        return "redirect:index";
        //System.out.println(book.getName());
        //System.out.println(book.getPrice());
        //return null;
    }
}
